import cTitle from "components/title";
import { Toast } from 'vant';
import { scrollMixin } from "utils/mixin";
import BScroll from "better-scroll";
export default {
  // mixins: [scrollMixin], //加载更多
  data() {
    return {
      show_num:'',
      keyword:'',
      arr:[],
      datas: [],
      goload: true,
      page: 1,
      total_page: 0,
      result:[],
      store_id:'',
      first:'',
      second:'',
      option1: [],
      option2: [
      ],
      activeNames: "",
      showPopup: false,
      chooseGoods: [], //选中的商品
    };
  },
  activated() {
    this.initData();
    this.store_id=this.$route.params.id;
    this.getNum();
    this.getNetData();
  },
  methods: {
    getNum(){
      let that = this;
      let json = {store_id:this.store_id };
      $http
        .get(
          "plugin.store-cashier.frontend.store.near-by-goods.get-store-goods",
          json,
          "获取中"
        )
        .then(
          response => {
            if (response.result == 1) {
              that.show_num=response.data.show_num;
            } else {
              that.$dialog.alert({message:response.msg});

            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    toSearch(){
      this.$router.push(this.fun.getUrl('nearSearch',{keyword:this.keyword,store_id:this.store_id}));
    },
    initData(){
      this.show_num='';
      this.arr=[];
      this.datas=[];
      this.goload=true;
      this.page=1;
      this.total_page=0;
      this.result=[];
      this.store_id='';
      this.first='';
      this.second='';
      this.option1=[];
      this.option2=[];
    },
    add(){
      let json={store_id:this.store_id,choose_goods:this.result};
      $http
        .post(
          "plugin.nearby-store-goods.frontend.controllers.goods.index",
          json,
          "获取中"
        )
        .then(
          response => {
            if (response.result == 1) {
              Toast(response.msg);
              this.$router.go(-1);
            } else {
              Toast(response.msg);
            }
          });
    },
    changeSecond(value,index){
      this.second = value;
      for(let item of this.option2) {
        this.$set(item, "actived" , false);
      }
      this.$set(this.option2[index], "actived" , true);
      // this.result=[];
      this.page=1;
      let that = this;
      let json = { store_id: this.store_id,page:this.page,category:{
        parentid:this.first,
        childid:this.second
      }};
      $http
        .post(
          "plugin.store-cashier.frontend.store.near-by-goods.get-goods-list",
          json,
          "获取中"
        )
        .then(
          response => {
            if (response.result == 1) {
              that.datas=response.data.list.data;
              for(let item of that.result) {
                for(let citem of that.datas) {
                  if(item == citem.id) {
                    that.$set(citem, "isAdd" , true);
                  }
                }
              }
              that.total_page = response.data.list.last_page;
              if (!that.total_page) {
                that.total_page = 0;
              }
              that.activeNames = false
            } else {
              Toast(response.msg);
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    changeCategory(value,index){
      for(let item of this.option1) {
        this.$set(item, "actived" , false);
      }
      this.$set(this.option1[index], "actived" , true);
      this.page=1;
      // this.result=[];
      this.option2=[];
      this.arr.forEach((item,index,key)=>{
        if(item.id==value){
          let obj=item.childrens;
          obj.forEach((list,index,key)=>{
            let o={};
            o.text=list.name;
            o.value=list.id;
            this.option2.push(o);
          });
        }
      });
      this.option2.unshift({
        text: "全部",
        value: ""
      })
      this.$set(this.option2[0], "actived" , true);
      let that = this;
      this.first = value;
      let json = { store_id: this.store_id,page:this.page,category:{
        parentid:this.first,
        childid:''
      }};
      $http
        .post(
          "plugin.store-cashier.frontend.store.near-by-goods.get-goods-list",
          json,
          "获取中"
        )
        .then(
          response => {
            if (response.result == 1) {
              that.datas=response.data.list.data;
              for(let item of this.result) {
                for(let citem of that.datas) {
                  if(item == citem.id) {
                    this.$set(citem, "isAdd" , true);
                  }
                }
              }
              that.total_page = response.data.list.last_page;
              if (!that.total_page) {
                that.total_page = 0;
              }
            } else {
              Toast(response.msg);
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    getNetData() {
      let that = this;
      let json = { store_id: this.store_id,page:this.page};
      $http
        .post(
          "plugin.store-cashier.frontend.store.near-by-goods.index",
          json,
          "获取中"
        )
        .then(
          response => {
            if (response.result == 1) {
              that.arr=response.data.category;
              that.datas=response.data.goods_data.data;
              that.total_page = response.data.goods_data.last_page;
              if (!that.total_page) {
                that.total_page = 0;
              }
              that.arr.forEach((item,index,key)=>{
                let json={};
                json.text=item.name;
                json.value=item.id;
                that.option1.push(json);
              });
              if(that.option1.length) {
                this.changeCategory(that.option1[0].value,0)
              }

              if (!this.foodScroll) {
                  setTimeout(() => {
                    this.foodScroll = new BScroll(this.$refs.goodsWrapper, {
                      click: true,
                      scrollY: true,
                      probeType: 3,
                      pullUpLoad: true,
                    });
                    this.foodScroll.on("pullingUp", () => {
                      this.getScrollData();
                      setTimeout(() => {
                        this.foodScroll.finishPullUp();
                        // //并且重新计算滚动区域的高度
                        this.foodScroll.refresh();
                      }, 500);
                    })
                  },500)

              } else {
                this.foodScroll.refresh();
              }
              if (!this.sortWrapper) {
                setTimeout(() => {
                  this.sortWrapper = new BScroll(this.$refs.sortWrapper, {
                    click: true,
                    scrollY: true,
                    probeType: 3,
                    pullUpLoad: true,
                    mouseWheel: true
                  });
                },500)

            } else {
              this.sortWrapper.refresh();
            }
            } else {
              // 判断权限
              if (response.data.url) {
                that.$notify(response.msg);
                window.location.href = response.data.url;
                return 0;
              } else {
                // that.$message.error(response.msg);
                // that.$message.error("暂无商品分类,请添加分类");
                that.$notify("暂无商品分类,请添加分类");
              }
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    getScrollData() {
      var that = this;
      let url='';
      let json={};
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        json = { page: this.page,store_id: this.store_id,category:{
          parentid:this.first,
          childid:this.second
        }};
          url= "plugin.store-cashier.frontend.store.near-by-goods.get-goods-list";
        $http
          .post(
            url,
            json,
            "加载中"
          )
          .then(
            function(response) {
              if (response.result === 1) {
                  that.datas = that.datas.concat(response.data.list.data);
                  // console.log(that.result,that.datas,5555);
                  for(let item of that.result) {
                    for(let citem of that.datas) {
                      if(item == citem.id) {
                        that.$set(citem, "isAdd" , true);
                      }
                    }
                  }
              } else {
                that.page = that.page - 1;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    },
    // 选中商品
    clickAdd(index) {
      this.$set(this.datas[index],"isAdd",true);
      let newChooseGoods = this.datas.filter(item  => item.isAdd);
      this.chooseGoods = this.reduceData([...this.chooseGoods,...newChooseGoods]);
      this.result = this.chooseGoods.map(citem => citem.id);
    },
    // 删除商品
    clickDel(index,id) {
      this.$set(this.datas[index],"isAdd",false);
      let index_ = ""
      for(let [key , item] of this.chooseGoods.entries()) {
        if(item.id == id) {
          index_ = key;
        }
      }
      this.chooseGoods.splice(index_,1);
      this.result = this.chooseGoods.map(citem => citem.id);
    },
    // 删除选中的数据
    delChooseGoods(index,id) {
      this.chooseGoods.splice(index,1);
      for(let [index_ , item] of this.datas.entries()) {
        if(item.id == id) {
          this.$set(this.datas[index_],"isAdd",false);
        }
      }
      this.result = this.chooseGoods.map(citem => citem.id);
      // this.result.push(...newArr);
    },
    // 清空选中数据
    clearChooseGoods() {
      this.chooseGoods = [];
      for(let [index_ , item] of this.datas.entries()) {
        this.$set(this.datas[index_],"isAdd",false);
      }
      this.result = [];
    },
    // 过滤重复数据
    reduceData(person) {
      let obj = {};
      let peon = person.reduce((cur,next) => {
          obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
          return cur;
      },[]) //设置cur默认类型为数组，并且初始值为空的数组
      return peon
    }
  },
  components: {
    cTitle
  }
};
